package test;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.UUID;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ServerDemo {

    public static void main(String[] args) throws IOException {
        ExecutorService service = Executors.newCachedThreadPool();
        ServerSocket ss = new ServerSocket(10000);
        while (true) {
            Socket accept = ss.accept();
            service.submit(new Runnable() {
                @Override
                public void run() {
                    try {
                        InputStream is = accept.getInputStream();

                        String uuid = UUID.randomUUID().toString();
                        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("internet-test\\src\\" + uuid + ".txt"));
                        int b;
                        while ((b = is.read()) != -1) {
                            bos.write(b);
                        }
                        bos.close();
                        System.out.println("复制文件成功");

                        OutputStream os = accept.getOutputStream();
                        os.write("反馈".getBytes());
                        os.flush();
                        accept.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            });
        }

//        ss.close();

    }
}
